using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CodePlex.Resourceful.Json
{
    public class JsonBuilder
    {
        private readonly StringWriter _rt;
        private readonly JsonWriter _writer;
        public JsonBuilder()
        {
            _rt = new StringWriter();
            _writer = new JsonWriter(_rt);

        }

        public JsonWriter Writer
        {
            get
            {
                return _writer;
            }
        }

        public IDisposable StartObject()
        {
            _writer.WriteStartObject();

            return new QuickDisposable(delegate
            {
                _writer.WriteEndObject();
            });
        }

        public IDisposable StartArray()
        {
            _writer.WriteStartArray();

            return new QuickDisposable(delegate
            {
                _writer.WriteEndArray();
            });
        }

        public override string ToString()
        {
            return _rt.ToString();
        }



        public void WritePropertyName(string name)
        {
            _writer.WritePropertyName(name);
        }

        public void WriteProperty(string name, string value)
        {
            _writer.WritePropertyName(name);
            _writer.WriteValue(value);
        }

        public void WriteProperty(string name, int value)
        {
            _writer.WritePropertyName(name);
            _writer.WriteValue(value);
        }

        public void WriteProperty(string name, bool value)
        {
            _writer.WritePropertyName(name);
            _writer.WriteValue(value);
        }


        public void WriteProperty(string name, object[] value)
        {
            if (value.Length != 0)
            {
                throw new NotSupportedException();
            }

            _writer.WritePropertyName(name);
            _writer.WriteStartArray();
            _writer.WriteEndArray();
        }

        public void WriteProperty(string name)
        {
            _writer.WritePropertyName(name);
            _writer.WriteNull();
        }
    }


}
